package com.andnnl.http;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;

import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;


/**
 * @author 架构师小跟班
 * @Description: https://www.jiagou1216.com
 * @date 2020/7/8 19:14
 */
public class ProxyTest {
    /**
     * 访问显示使用IP
     */
    private static String url = "https://api.ipify.org/?format=json%27";

    public static void main(String[] args) {
//        normal();
        proxy();
//        testParse();

    }
/*
    private static void testParse() {
        //{"code":"0000","msg":"发送成功","data":null,"success":true}
//        String json="{\"code\":\"0000\",\"msg\":\"发送成功\",\"data\":null,\"success\":true}";
        String json="{\"code\":\"0000\",\"msg\":\"发送成功\",\"data\":null,\"success\":\"true\"}";
//        String json=null;
        Map map= JsonLib.toObject(json, HashMap.class);

        if(map!=null && "true".equals(""+map.get("success"))){
            System.out.println(true);
        }else{
            System.out.println(false);
        }

        if(map!=null && Boolean.parseBoolean(""+map.get("success"))){
            System.out.println(true);
        }else{
            System.out.println(false);
        }

        if(map!=null && Boolean.valueOf(""+map.get("success"))){
            System.out.println(true);
        }else{
            System.out.println(false);
        }

//        if(map!=null && (boolean) map.getOrDefault("success",false)){
//            System.out.println(true);
//        }else{
//            System.out.println(false);
//        }

        Boolean x = Optional.ofNullable(map)
                .map(m -> m.getOrDefault("success", "false").toString())
                .map(s -> Boolean.valueOf(s)).orElse(getFalse());
        System.out.println(x);

        Boolean x2 = Optional.ofNullable(map)
                .map(m -> m.getOrDefault("success", "false").toString())
                .map(s -> Boolean.valueOf(s)).orElseGet(()->Boolean.FALSE);
        System.out.println(x2);

        System.out.println(Boolean.TRUE== MapUtil.getBool(map,"success"));

        ObjectUtil.isEmpty(null);
        System.out.println(RandomUtil.randomInt(10,100));
    }*/

    private static Boolean getFalse(){
        return false;
    }
    /**
     * 正常访问
     */
    public static void normal() {
        System.out.println("normal:"+ HttpUtil.get(url));
    }

    /**
     * 代理访问
     */
    public static void proxy() {
        System.out.println(System.currentTimeMillis());
        Date d=new Date(1608020511L*1000);
        System.out.println(DateUtil.format(d,"yyyy-MM-dd HH:mm:ss"));
//        String ip = "58.220.95.42";
//        int port = 10174;
        String ip = "192.168.4.164";
        int port = 55143;
//        Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(ip, port));
        Proxy proxy=null;
        String result = HttpRequest.get(url)
//                .setConnectionTimeout(3000)
//                .setFollowRedirects(true)
//                .setProxy(proxy)
                .execute().body();
        System.out.println("proxy:"+result);
    }


    /**
     * 代理访问
     */
    public static void proxy(String ip,int port) {
        try {
            System.out.println(System.currentTimeMillis());
            Date d = new Date(1608020511L * 1000);
            System.out.println(DateUtil.format(d, "yyyy-MM-dd HH:mm:ss"));
//        String ip = "58.220.95.42";
//        int port = 10174;
//        String ip = "192.168.4.164";
//        int port = 55143;
            Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(ip, port));
            String result = HttpRequest.get(url)
                    .setConnectionTimeout(3000)
                    .setFollowRedirects(true)
                    .setProxy(proxy)
                    .execute().body();
            System.out.println("proxy:" + result);
        }catch (Exception e){

        }
    }



}
